package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.ConfigUtil;

import com.util.BODY;
import com.util.HttpRequest;
import com.util.JSONUtil;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;

/**
 * 在线还款
 * @author 
 */
@Controller
@RequestMapping("/zxhkcontroller")
public class zxhkcontroller {

	private static final Logger logger = Logger.getLogger(zxhkcontroller.class);
	Map map  = new HashMap();
	List list = new ArrayList();
	  
	@RequestMapping("/test")
	@ResponseBody
	public String entryinterface(HttpServletRequest request, HttpServletResponse response , String limit ,String id,String start,String end,String JKRXM,HttpSession session) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		//读取配置文件路劲
		String interfaceUrl = ReadConfig.readValue();
		Map reqMap = new HashMap();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ148");
		//接口参数
		reqMap.put("OpenKey",sessionIn.getKEY());
		BODY b=new BODY();
		b.setZJHM(sessionIn.getZJHM());
		reqMap.put("BODY",b);
		OpenKey opk=new OpenKey();
		String result=opk.getOpenKey(reqMap, session);
		Map map  = new HashMap();
		logger.info("接口返回内容:"+result);
		if(result != null){
			Map resMap = JSONUtil.toMap(result);
			if(resMap.get("RETURNCODE").equals("100")){
				Map mx=JSONUtil.toMap(JSONUtil.ToJsongString2(resMap.get("body")));
				map.put("code", 0);
				map.put("msg", "1");
				map.put("data", mx.get("RETDATA"));
				list.add(resMap);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}else{
				map.put("code", 1);
				map.put("msg", "1");
				map.put("CW", resMap.get("RETURNDESC").toString());
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
				return null;
			}
		}
		return null;
	}
	//在线还款controller
	@RequestMapping("/test1")
	@ResponseBody
	public String entryinterface1(HttpServletRequest request, HttpServletResponse response , String limit,HttpSession session,String id,String JKRXM,String yhrq,String hkje,String hkbj,String fx,String sjhkje,String dqdm,String gsdqdm,String hkjhlsh,String dkzh,String HKLX,String DKZH,String DQDM,String GSDQDM) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		//读取配置文件路劲
		if(yhrq!=null)yhrq=yhrq.replaceAll("-","");
		String interfaceUrl = ReadConfig.readValue();
		Map reqMap = new HashMap();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ164");
		//接口参数
		reqMap.put("OpenKey",sessionIn.getKEY());
		BODY b=new BODY();
		b.setGRZH(sessionIn.getGRZH());
		if(id !=null && id !=""){
			b.setID(id);
			b.setYHRQ(yhrq);
			b.setHKJE(hkje);
			b.setHKBJ(hkbj);
			b.setFX(fx);
			b.setHKJHLSH(hkjhlsh);
			b.setSJHKJE(sjhkje);
			b.setDKZH(DKZH);
			b.setGSDQDM(GSDQDM);
			b.setDQDM(DQDM);
			b.setCZY("网厅");
		}
		reqMap.put("BODY",b);	
		OpenKey opk=new OpenKey();
		String result=opk.getOpenKey(reqMap, session);
		logger.info("YBGJJ164接口返回内容:"+result);
		Map resMap = JSONUtil.toMap(result);
		if(result != null){
			if(resMap.get("RETURNCODE").equals("100")){
				Map mx=JSONUtil.toMap(JSONUtil.ToJsongString2(resMap.get("body")));
				map.put("code", 0);
				map.put("msg", "1");
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}else{
				map.put("code", 1);
				map.put("msg", "1");
				map.put("CW", resMap.get("RETURNDESC").toString());
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
				return null;
			}
		}else{
			map.put("code", 1);
			map.put("msg", "1");
			map.put("CW", resMap.get("RETURNDESC").toString());
			response.setHeader("Content-type", "text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			String jsonString = JSONUtil.ToJsongString2(map);
			PrintWriter pw = response.getWriter();
			pw.write(jsonString);
			return null;
		}
		return null;
	}
}
